#!/bin/sh

BASEDIR=`dirname $0`
VERSION=${1:?no version specified}
BRANCH=$2

if [ -z $BRANCH ]
then
    if [ "`svn st -q`" != "" ];
    then
        echo "uncommitted changes in working copy: commit changes or specify explicit revision"
        exit 1
    fi

    svn up
    BRANCH=trunk@`svn info | grep "Revision:" | cut -c 11-`
fi

SVN_ROOT=`svn info | grep "Repository Root:" | cut -c 18-`

echo releasing $SVN_ROOT/$BRANCH as version $VERSION

svn cp $SVN_ROOT/$BRANCH $SVN_ROOT/tags/$VERSION -m "releasing version $VERSION"
if [ $? -ne 0 ]
then
    exit 1
fi

mkdir -p $BASEDIR/out/release
rm -rf $BASEDIR/out/release/*
svn export $SVN_ROOT/tags/$VERSION $BASEDIR/out/release/$VERSION
if [ $? -ne 0 ]
then
    exit 1
fi

(cd $BASEDIR/out/release/$VERSION; ant -Dversion=$VERSION)
if [ $? -ne 0 ]./out/release/1.1.0/*.zip
then
    exit 1
fi

# Todo - automatically upload to Google project
echo upload `ls $BASEDIR/out/release/$VERSION/out/*.zip`
